<?php

class Core_Frontend_Component_Article_Controller_Comment extends Core_Frontend_Share_Component_Controller {

    public function comlistAction($id, $page = 1) {
        $comments = App::getModel('article/collection_comment')
                        ->filterBy('articleid', $id)
                        ->filterBy('status', 'published')
                        ->sortBy('time', 'desc')
                        ->pagination($page, 20)->load();
        $records = $comments->loadCount();
        $args = array_merge($this->getRequest()->getAllParameters(), array('page' => '{index}'));
        $pagination = new Kd_Html_Pagination();
        $pagination->setPageIndex($page)
                ->setPageSize(20)
                ->setPageRange(6)
                ->setRecordCount($records)
                ->setUrlFormat($this->getUrl($args));
        $this->getVariables()->setData('comment_article_id', $id);
        $content = App::getModel('article/content')->load($id);
        Kd_Web::getPage()->getNavigation()->addLink('新闻列表', $this->getUrl(NULL, 'conlist', 'content'))
                ->addlink($content->getTitle(), $this->getUrl(array('id' => $id), 'detail', 'content'))
                ->addLabel('评论列表');
        Kd_Web::getPage()->setTitle('评论列表')->setKeyword('');
        return $this->viewResult(array('comments' => $comments, 'content' => $content, 'pagination' => $pagination));
    }

    public function commentFormPostAction($id, $page = 1) {
        $validateCode = $this->getRequest()->getForm('validatecode');
        if (strcasecmp($validateCode, Kd_Web::getSession()->get('validate_code')) != 0) {
            $this->getMessager()->addError($this->__('agent.label.error_validateimg'));
            return $this->redirectResult($_SERVER['HTTP_REFERER']);
        }
        if (!Kd_Web::getIdentity()->isLogin()) {
            $name = $this->getRequest()->getForm('name','游客');
        }
        else{
            $name = $this->getIdentity()->getName();
        }
        $comment = $this->getRequest()->getForm('detail');
        App::getModel('article/comment')
                ->setDetail($comment)
                ->setAccount($name)
                ->setTime(date('Y-m-d H-i-s'))
                ->setIp($_SERVER['REMOTE_ADDR'])
                ->setStatus('published')
                ->setArticleId($id)
                ->save();
        return $this->redirectResult($_SERVER['HTTP_REFERER']);
    }

    public function updateSupportAction($id) {
        $comment = App::getModel('article/comment')->load($id);
        $comment -> updateSupport();
    }

    public function updateOppositionAction($id) {
        $comment = App::getModel('article/comment')->load($id);
        $comment -> updateOpposition();
    }

}